﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OrmSmackdown.LLBLGen.EntityClasses;
using OrmSmackdown.LLBLGen.Linq;

namespace OrmSmackdown.LLBLGen
{
    public class Crud : ICrud
    {
        private LinqMetaData ctx;
        public Crud()
        {
            ctx = new LinqMetaData();
        }

        public int Create(string firstName, string lastName, string email)
        {
            var contact1 = new ContactEntity { FirstName = firstName, LastName = lastName, Email = email };

            contact1.Save();

            return contact1.PersonId;
        }

        public IPerson Read(int id)
        {
            return ctx.Contact.Where(p => p.PersonId == id).FirstOrDefault() as IPerson; 
        }

        public void Update(int id, string firstName, string lastName, string email)
        {
            var person = ctx.Contact.Where(p => p.PersonId == id).FirstOrDefault();
            person.FirstName = firstName;
            person.LastName = lastName;
            person.Email = email;
            person.Save();
        }

        public void Delete(int id)
        {
            var person = ctx.Contact.Where(p => p.PersonId == id).FirstOrDefault();
            person.Delete();
        }


        public void Dispose()
        {
            
        }
    }
}
